package net.stevechaloner.idea.ffs.config;

import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;

import net.stevechaloner.idea.util.properties.DOMable;
import net.stevechaloner.idea.util.properties.DOMablePropertyContainer;
import net.stevechaloner.idea.util.properties.PropertyContainer;
import net.stevechaloner.idea.util.properties.DOMableGeneric;
import net.stevechaloner.idea.util.properties.DOMableList;
import net.stevechaloner.idea.util.paths.Path;

/**
 * @author Steve Chaloner
 */
public class Config implements DOMable
{
    /**
     * Key for the search plugin directory.
     */
    private static final String SEARCH_PLUGINS_DIR = "search-plugin-dir";

    /**
     * Key for the quoted search providers.
     */
    private static final String QUOTED_SEARCH_PROVIDERS = "quoted-search-providers";

    /**
     * The persistence model.
     */
    private final DOMable domable;

    /**
     * The simple properties.
     */
    private final PropertyContainer<String, DOMable> propertyContainer;

    /**
     *
     */
    public Config()
    {
        DOMablePropertyContainer dpc = new DOMablePropertyContainer("config",
                                                                    new HashMap<String, DOMable>()
        {{
            put(SEARCH_PLUGINS_DIR,
                new DOMableGeneric<String>(SEARCH_PLUGINS_DIR));
            put(QUOTED_SEARCH_PROVIDERS,
                new DOMableList<String>(QUOTED_SEARCH_PROVIDERS));
        }});

        this.domable = dpc;
        this.propertyContainer = dpc;
    }

    // javadoc unnecessary
    public String getSearchPluginsDirectory()
    {
        return (String)propertyContainer.get(SEARCH_PLUGINS_DIR).getValue();
    }

    // javadoc unnecessary
    public void setSearchPluginsDirectory(String searchPluginsDirectory)
    {
        DOMableGeneric<String> value = (DOMableGeneric<String>)propertyContainer.get(SEARCH_PLUGINS_DIR);
        value.setValue(checkPath(searchPluginsDirectory));
    }

    // javadoc unnecessary
    public List<String> getQuotedSearchProviders()
    {
        return ((DOMableList<String>)propertyContainer.get(QUOTED_SEARCH_PROVIDERS)).getValue();
    }

    public void setQuotedSearchProviders(List<String> quotedSearchProviders)
    {
        DOMableList<String> value = (DOMableList<String>)propertyContainer.get(QUOTED_SEARCH_PROVIDERS);
        List<String> list = value.getValue();
        list.clear();
        list.addAll(quotedSearchProviders);
    }

    /**
     * Normalises the path to deal with Windows paths.
     *
     * @param searchPluginDir the path
     * @return the normalised path
     */
    private static String checkPath(String searchPluginDir)
    {
        return Path.normalisePath(searchPluginDir);
    }

    // javadoc inherited
    @NonNls
    @NotNull
    public String getName()
    {
        return domable.getName();
    }

    // javadoc inherited
    @NotNull
    public Element write()
    {
        return domable.write();
    }

    // javadoc inherited
    public void read(@NotNull Element element)
    {
        domable.read(element);
    }

    public Object getValue()
    {
        return null;
    }
}
